var Kuo_Bat_login = {
    Gateway:{},//前置网关
    USER:{},
    FID:{},
    ServerId:0,
    GateUpdate:async ()=>{
        let $db = await db("gateway");
        let shuju = await $db.ZhiCha("id,udpprot,udpip").Where({type:0,off:1}).Select();
        if(shuju){
            var shuju_ ={};
            for(var x in shuju){
                shuju_[shuju[x].id] = shuju[x]
            }
            return shuju_;
        }
        return {};
    },
    $DATA:{
        code:0,
        data:null,
        msg:"",
        token:""
    },FASONG:(token,$E,$L)=>{
        let THIS = Kuo_Bat_login;
        let chanfei = 2048;
        if(typeof(token) == "object" ){
            var tongji = Buffer.byteLength(""+token, 'utf8');
            var fenzu = {};
            var SHUJU = {};
            if(tongji > chanfei){
                let leiji = 0;
                let chafenzu =[];
                for(var xx in token){
                    chafenzu.push(token[xx]);
                    leiji +=Buffer.byteLength(""+token[xx], 'utf8');
                    if( leiji >= chanfei ){
                        leiji = 0;
                        fenzu[xx] = chafenzu;
                        chafenzu =[];
                    }
                }
                if(chafenzu && chafenzu.length > 0){
                    fenzu[xx+1] = chafenzu;
                }
            }else{
                fenzu[1] = token;
            }
            var datatj = Buffer.byteLength(""+$E+$L, 'utf8');
            if(datatj > chanfei &&  typeof($L) != "string" && typeof($L[0]) != "undefined"){
                let chafenzu =[];
                let leiji = 0;
                for(var xx in $L){
                    chafenzu.push($L[xx]);
                    leiji +=Buffer.byteLength(""+$L[xx], 'utf8');
                    if( leiji >= chanfei ){
                        SHUJU[xx] = chafenzu;
                        leiji = 0;
                        chafenzu =[];
                    }
                }

                if(chafenzu && chafenzu.length > 0){
                    SHUJU[xx+1] = chafenzu;
                }

            }else{
                SHUJU[1] = $L;
            }

            for(var xx in fenzu){
                let userzu = fenzu[xx];
                for(var xx in SHUJU){
                    let shuju_ = SHUJU[xx];
                    THIS.FASONG_(userzu,$E,shuju_);
                }
            }

        }else{

            var datatj = Buffer.byteLength(""+$E+$L, 'utf8');
            if(datatj > chanfei &&  typeof($L) != "string" &&  typeof($L[0]) != "undefined"){
                var SHUJU = {};
                let chafenzu =[];
                let leiji = 0;
                for(var xx in $L){
                    chafenzu.push($L[xx]);
                    leiji +=Buffer.byteLength(""+$L[xx], 'utf8');
                    if( leiji >= chanfei ){
                        SHUJU[xx] = chafenzu;
                        leiji = 0;
                        chafenzu =[];
                    }
                }
                if(chafenzu && chafenzu.length > 0){
                    SHUJU[xx+1] = chafenzu;
                }
                for(var xx in SHUJU){
                    let shuju_ = SHUJU[xx];
                    THIS.FASONG_(token,$E,shuju_);
                }

            }else{
                THIS.FASONG_(token,$E,$L);
            }
        }

    },FASONG_:(token,$E,$L)=>{
        var TONG = {};
        TONG["E"] = $E;
        
        if($L){
            TONG["L"] = $L;
        }
        TONG['EE'] = 0;
        TONG['EF'] = [];
        let THIS = Kuo_Bat_login;
        if(typeof(token) != "object" ){

            if( isNaN(token)){
                var FUU = THIS.FID[token];
                if(FUU){
                    let ipport =  THIS.Gateway[FUU.EB];
                    p(ipport);
                    TONG['EE'] = FUU.EE;
                    UDPSERVER.send(MSGPACK.encode(TONG ),ipport.udpprot, ipport.udpip);
                    return ;
                }

            }else{

                TONG['EE'] = Mode("Tools").Int(token);
                if(TONG['EE'] == -1){
                    TONG['EE'] = - THIS.ServerId;
                }
                var dd = MSGPACK.encode(TONG);
                for( var fuid_ in THIS.Gateway){
                    var ipport =  THIS.Gateway[fuid_];
                    UDPSERVER.send(dd,ipport.udpprot, ipport.udpip);
                }
                return ;
            }
        }else{
            var $FASONG = {};
            for(var x in token){
                let uid = token[x];
                if(THIS.USER[uid]){
                    let FUU = THIS.FID[THIS.USER[uid]];
                    if(FUU){
                        if(!$FASONG[FUU.EB]){
                            $FASONG[FUU.EB] = [];
                        }
                        if(! $FASONG[FUU.EB].includes(FUU.EE)){
                            $FASONG[FUU.EB].push(FUU.EE);
                        }
                    }
                }
            }
            for(var bid in $FASONG){
                if($FASONG[bid] ){
                    let ipport =  THIS.Gateway[bid];
                    TONG['EF'] = $FASONG[bid];
                    UDPSERVER.send(msgpack.encode(TONG),ipport.udpprot, ipport.udpip);
                }
            }
        }
    }
}